﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;


namespace Ccit.OA.Email
{
    public partial class email_wdyj_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ViewState["Id"] = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "";
                InitPage();
            }
        }

        public void InitPage()
        { 
            string sId = ViewState["Id"].ToString().Trim();

            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            hidYGJB.Value = loginUser.ClassId;
            hidYGBM.Value = loginUser.DeptId;
            hidYGZW.Value = loginUser.PosId;
            hidJG.Value = loginUser.OrganId;
            hidTime.Value = DateTime.Now.ToString().Trim();
            hidUserId.Value = loginUser.UserId;
            if (sId != "")
            {
                hidId.Value = sId;
                EmailBLL bll = new EmailBLL();
                MsEmailSigna msSigna = new MsEmailSigna();
                msSigna.userId = hidUserId.Value;
                msSigna.Id = sId;
                DataSet dSet = bll.GetSigna(msSigna);
                if(dSet.Tables[0].Rows.Count != 0)
                {
                    txtTitle.Text = dSet.Tables[0].Rows[0]["EML_A5_30"].ToString();
                    txtContent.Text = dSet.Tables[0].Rows[0]["EML_A5_40"].ToString();
                }

                if (dSet.Tables[0].Rows[0]["EML_A5_50"].ToString().Trim() == "1")
                {
                    cbMR.Checked = true;
                    cbMR.Value = "1";
                }

                bll = null;
                msSigna = null;

            }
        }

        /// <summary>
        /// 删除ajax方法 邮件夹下边所有邮件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string Save(string serExpr)
        {
            bool bFlag = false;
            string operType = string.Empty;
            MsEmailSigna msSigna = (MsEmailSigna)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailSigna));
            EmailBLL bll = new EmailBLL();
            if (msSigna.Id == "")
            {
                msSigna = bll.SaveSigna(msSigna);
                if (msSigna.Id != "")
                {
                    bFlag = true;
                }
                operType = "insert";
            }
            else
            {
                bFlag = bll.UpdateSigna(msSigna);
                operType = "update";
            }
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult,resultSignId = msSigna.Id,resultName = msSigna.title };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msSigna = null;
            return resultSerExpr;
        }



        /// <summary>
        /// 删除ajax方法 删除签名
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string Del(string serExpr)
        {
            bool bFlag = false;
            string operType = string.Empty;
            MsEmailSigna msSigna = (MsEmailSigna)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailSigna));
            EmailBLL bll = new EmailBLL();

            bFlag = bll.DelSign(msSigna);
            operType = "Del";
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult, resultSignId = msSigna.Id};
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msSigna = null;
            return resultSerExpr;
        }
    }
}
